<?php
/**
 * Created by PhpStorm.
 * User: ari
 * Date: 10/17/14
 * Time: 10:47 PM
 */
namespace BC\Games\Holdem\Hands;

use BC\Games\Holdem\Cards;
use BC\Games\Holdem\Deck;
use CPath\UnitTest\ITestable;
use CPath\UnitTest\IUnitTestRequest;

class FlushHand implements IPokerHand, ITestable
{
	const RANK = 5000;

	private $mCards;

	public function __construct(Array $cards) {
		sort($cards);
		$this->mCards = $cards;
	}

	/**
	 * Return name of this hand
	 * @return String
	 */
	function getName() {
		return "Flush";
	}

	function getCards($withNullDelimiter=false) {
		return $this->mCards;
	}

	/**
	 * Get Hand rank
	 * @return int
	 */
	function getRank() {
		return self::RANK
		+ ($this->mCards[0] & Cards::NUMBER_MASK);
	}

	/**
	 * Compare two hands
	 * @param IPokerHand $Hand
	 * @return int 0, <0, or >0
	 */
	function compare(IPokerHand $Hand) {
		$rank = $this->getRank() - $Hand->getRank();
		if (!$Hand instanceof FlushHand)
			return $rank;

		foreach ($this->mCards as $i => $card) {
			$rank = $card - $Hand->mCards[$i];
			if ($rank !== 0)
				return $rank;
		}

		return 0;
	}

	/**
	 * Returns a string representation of this poker hand
	 * @return String
	 */
	function __toString() {
		return '[' . Cards::printCards($this->mCards). ']';
	}

	// Static

	/**
	 * Tests a poker hand to see if it matches an array of cards
	 * @param array $cards
	 * @param IPokerHand[] $Matches
	 * @return bool true if the set of cards matches this poker hand
	 */
	static function match(Array $cards, &$Matches = array()) {
		$found = false;

		$suits = array();
		foreach($cards as $card) {
			$suit = $card & Cards::SUIT_MASK;
			if(!isset($suits[$suit]))
				$suits[$suit] = array($card);
			else
				$suits[$suit][] = $card;
		}

		foreach($suits as $suit=>$cards) {
			if(sizeof($cards) >= 7) {
				for($i=0; $i<=6; $i++) {
					for($j=0; $j<=6; $j++) {
						$cards2 = array();
						if($i===$j)
							continue;
						foreach($cards as $k=>$c)
							if($k !== $i && $k !== $j)
								$cards2[] = $c;
						$Matches[] = new FlushHand($cards2);
						$found = true;
					}
				}

			} elseif(sizeof($cards) >= 6) {
				for($i=0; $i<=5; $i++) {
					$cards2 = array();
					foreach($cards as $j=>$c)
						if($i !== $j)
							$cards2[] = $c;
					$Matches[] = new FlushHand($cards2);
					$found = true;
				}

			} elseif (sizeof($cards) == 5) {
				$Matches[] = new FlushHand($cards);
				$found = true;

			}
		}

		return $found;
	}

	/**
	 * Perform a unit test
	 * @param IUnitTestRequest $Test the unit test request inst for this test session
	 * @return void
	 * @test --disable 0
	 * Note: Use doctag 'test' with '--disable 1' to have this ITestable class skipped during a build
	 */
	static function handleStaticUnitTest(IUnitTestRequest $Test) {
		$Deck = new Deck();
		$Deck->replay(Cards::ACE | Cards::HEARTS, 'player1');
		$Deck->replay(Cards::FIVE | Cards::CLUBS, 'player1');
		$Deck->replay(Cards::THREE | Cards::HEARTS, 'board');
		$Deck->replay(Cards::SEVEN | Cards::HEARTS, 'board');
		$Deck->replay(Cards::NINE | Cards::HEARTS, 'board');
		$Deck->replay(Cards::TWO | Cards::CLUBS, 'board');
		$Deck->replay(Cards::TWO | Cards::HEARTS, 'board');

		$cards = $Deck->getDrawnCards('player1', 'board');
		if(!FlushHand::match($cards, $Hands))
			$Test->fail("Flush not matched to cards: " . Cards::printCards($cards));

		$Test->log("Matched Flush to " . $Hands[0] . ' from ' . Cards::printCards($cards), $Test::VERBOSE);
	}
}